<?php
class ProductAction extends CommonAction{

	public function index(){

		//自定义
		$Product=M('Product');

		$condForm['product_no'] = $_POST['product_no'];
		$condForm['product_name'] = $_POST["product_name"];

		if(!empty($condForm['product_no'])){
			$condition['product_no'] = array('like',makeSqlLikeValue($condForm['product_no']));
		}

		if(!empty($condForm['product_name'])){
			$condition['product_name'] = array('like',makeSqlLikeValue($condForm['product_name']));
		}

		import("@.ORG.Page"); //导入分页类
		$count = $Product->count();    //计算总数
		$p = new Page ( $count, 5 );
		if(isset($condition)){
			$list=$Product->where($condition)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		}else{
			$list=$Product->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		}
		$p->setConfig('header','篇记录');
		$p->setConfig('prev',"<");
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
		$page = $p->show ();
		$this->assign("condForm",$condForm);
		$this->assign( "page", $page );
		$this->assign ( "list", $list );
		$this->display();
	}

	public function add(){
		$Category = M("Category");
		$parents = $Category->where("parent=0")->findAll();

		$Dict = D("Dict");
		$Dicts = $Dict->relation(true)->where('code="size"')->findAll();

		$this->assign("size",$Dicts[0]["DictMap"]);
		$this->assign("categories", $parents);
		$this-> display();
	}

	public function insert(){

		if(!empty($_FILES)){
			$uploadFileList = self::_upload($_POST, 'pic');
			$imgPath = $uploadFileList[0]['savename'];
			$_POST["pic"] = $imgPath;
		}

		$Product = D("Product");
		if($vo = $Product->create()) {
			//添加关联的商品类别
			$categoryIds = $_POST["list_category"];
			for ($i = 0;$i < count($categoryIds); $i++) {
				$mappings[] = array('id'=> $categoryIds[$i]);
			}
			$Product->Categories = $mappings;
			//添加关联的商品规格
			$SizeList = $_POST["size_list"];
			for ($i = 0;$i < count($SizeList); $i++) {
				$productSpecs[] = array('size'=> $SizeList[$i]);
			}
			$Product->Categories = $mappings;
			$Product->ProductSpecs = $productSpecs;
			//级联保存
			$list=$Product->relation(true)->add();
			if($list!==false){
				$this->success('数据保存成功！');
			}else{
				$this->error('数据写入错误！');
			}
		}else{
			$this->error($Form->getError());
		}
	}

	// 更新数据
	public function update() {
		$ParentCategory = M("Category");
		$ParentCategory->find($_POST["parent"]);
		$_POST["parent_name"] = $ParentCategory->parent_name;

		if(!empty($_FILES["pic_u"]["name"])){
			$uploadFileList = self::_upload($_POST, 'pic_u');
			$imgPath = $uploadFileList[0]['savename'];
			$_POST["pic"] = $imgPath;
		}

		$Product=	D("Product");
		if($vo = $Product->create()) {
			//添加关联的商品类别
			$categoryIds = $_POST["list_category"];
			for ($i = 0;$i < count($categoryIds); $i++) {
				$mappings[] = array('id'=> $categoryIds[$i]);
			}
			$Product->Categories = $mappings;
			//添加关联的商品规格,先删除以前的，再保存
			$PS = M("ProductSpec");
			$PS->where('product_id= '.$_POST["id"])->delete();

			$SizeList = $_POST["size_list"];
			for ($i = 0;$i < count($SizeList); $i++) {
				$productSpecs[] = array('size'=> $SizeList[$i]);
			}
			$Product->Categories = $mappings;
			$Product->ProductSpecs = $productSpecs;
			$list=$Product->relation(true)->save();
			if($list!==false){
				$this->assign("vo",$Product);
				$this->success('数据更新成功！');
			}else{
				$this->error("没有更新任何数据!");
			}
		}else{
			$this->error($Product->getError());
		}
	}

	// 编辑数据
	public function edit() {
		if(!empty($_GET['id'])) {
			$Product	=	D("Product");
			$vo = $Product->relation(true)->find($_GET['id']);
			if($vo) {
				$Category = M("Category");
				$parents = $Category->where("parent=0")->findAll();

				$Dict = D("Dict");
				$Dicts = $Dict->relation(true)->where('code="size"')->findAll();

				$this->assign("size",$Dicts[0]["DictMap"]);
				$this->assign("categories", $parents);
				$this->assign('vo',$vo);

				$this->display();
			}else{
				exit('编辑项不存在！');
			}
		}else{
			exit('编辑项不存在！');
		}
	}

	// 删除数据
	public function delete() {
		if(!empty($_POST['id'])) {
			$Product	=	D("Product");
			$result	=	$Product->relation(true)->delete($_POST['id']);

			if(false !== $result) {
				$this->ajaxReturn($_POST['id'],'删除成功！',1);
			}else{
				$this->error('删除出错！');
			}
		}else{
			$this->error('删除项不存在！');
		}
	}

}